package cn.avalon.pet.service.impl;

import cn.avalon.basic.service.BaiduAiAuditService;
import cn.avalon.basic.service.impl.BaseServiceImpl;
import cn.avalon.basic.utils.AjaxResult;
import cn.avalon.basic.utils.LoginContext;
import cn.avalon.org.domain.Employee;
import cn.avalon.org.mapper.EmployeeMapper;
import cn.avalon.pet.domain.Pet;
import cn.avalon.pet.domain.PetDetail;
import cn.avalon.pet.domain.PetOnlineAuditLog;
import cn.avalon.pet.mapper.PetDetailMapper;
import cn.avalon.pet.mapper.PetMapper;
import cn.avalon.pet.mapper.PetOnlineAuditLogMapper;
import cn.avalon.pet.service.IPetService;
import cn.avalon.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private BaiduAiAuditService baiduAiAuditService;

    @Override
    @Transactional
    public void save(Pet pet) {
        //添加宠物 - 自增id
        super.save(pet);

        //添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    @Transactional
    public void remove(Long id) {
        //删除宠物信息
        super.remove(id);
        //删除宠物详情
        petDetailMapper.removeByPetId(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        //修改宠物信息
        super.update(pet);

        //修改宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        //删除宠物信息
        super.patchDelete(ids);
        //删除宠物详情
        petDetailMapper.patchDelByPetId(ids);
    }

    /**
     * 上架方法
     * @param ids
     * @param request
     * @return
     */
    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //上架 -不能用批量操作，有上架审核
        for (Long id : ids) {
            // 上架自动审核文本
            Pet pet = petMapper.findById(id);
            String auditText = pet.getName();
            Boolean textBoo = baiduAiAuditService.textAudit(auditText);

            // 审核图片：多张图片resources
            String resources = pet.getResources();
            Boolean imageBoo = true;
            if(!StringUtils.isEmpty(resources)){
                imageBoo = baiduAiAuditService.imageAudit(resources);
            }

            //获取审核人
            Logininfo logininfo = LoginContext.getLogininfo(request);
            Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());

            //审核通过
            if (textBoo && imageBoo){
                //数据库操作
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                params.put("onsaletime",new Date());
                petMapper.onsale(params);
                //审核成功-note
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核成功！");
                petOnlineAuditLogMapper.save(auditLog);
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.save(auditLog);
                //审核失败，上架失败
                return AjaxResult.me().setSuccess(false).setMsg("上架失败！审核失败,宠物名称或图片不合法!!!");
            }
        }
        return AjaxResult.me();
    }

    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        petMapper.offsale(params);
        return AjaxResult.me();
    }
}
